package com.rkjoeson.player;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.OrientationHelper;

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import com.pili.pldroid.player.widget.PLVideoView;
import com.rkjoeson.player.adapter.ShortVideoAdapter;
import com.rkjoeson.player.constants.QiniuPlayerEventConstants;
import com.rkjoeson.player.data.ReactShortVideoData;
import com.rkjoeson.player.layout.ViewPagerLayoutManager;
import com.rkjoeson.player.view.RNTShortVideoList;

import java.util.ArrayList;
import java.util.Map;

public class RNTShortVideoViewManager extends SimpleViewManager<RNTShortVideoList> implements LifecycleEventListener {

    private ArrayList<ReactShortVideoData> sourceData = new ArrayList<>();
    private RNTShortVideoList rntShortVideoList;

    private final String REACT_CLASS = "RNTQiniuPlayer";

    private ThemedReactContext context;
    private RCTEventEmitter mEventEmitter;
    private ShortVideoAdapter shortVideoAdapter;

    @Override
    public void onHostResume() {

    }

    @Override
    public void onHostPause() {

    }

    @Override
    public void onHostDestroy() {
        context.removeLifecycleEventListener(this);
    }

    @NonNull
    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    @Nullable
    public Map getExportedCustomDirectEventTypeConstants() {
        MapBuilder.Builder<String, Map<String, String>> builder = MapBuilder.builder();
        for (String event : QiniuPlayerEventConstants.EVENT_LIST) {
            builder.put(event, MapBuilder.of("registrationName", event));
        }
        return builder.build();
    }

    @NonNull
    @Override
    protected RNTShortVideoList createViewInstance(@NonNull ThemedReactContext reactContext) {
        context = reactContext;
        // 注册生命周期
        context.addLifecycleEventListener(this);
        // 获取对应的json事件对象
        mEventEmitter = context.getJSModule(RCTEventEmitter.class);
        // 初始化对应的推流视图
        initRecycleView();
        return rntShortVideoList;
    }

    @Override
    public void onDropViewInstance(@NonNull RNTShortVideoList view) {
        super.onDropViewInstance(view);
    }


    /**
     * 获取视图id
     *
     * @return int
     */
    private int getId() {
        return rntShortVideoList.getId();
    }

    /**
     * 初始化配置
     */
    private void initRecycleView() {
        shortVideoAdapter = new ShortVideoAdapter(context, sourceData, mEventEmitter);
        shortVideoAdapter.setOnItemClickListener(new ShortVideoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });

        ViewPagerLayoutManager viewPagerLayoutManager = new ViewPagerLayoutManager(context, OrientationHelper.VERTICAL);
        viewPagerLayoutManager.setOnViewPagerListener(new ViewPagerLayoutManager.OnViewPagerListener() {
            @Override
            public void onInitComplete(View view) {
                rntShortVideoList.post(() -> {
                    PLVideoView plVideoView = view.findViewById(R.id.VideoView);
                    if (plVideoView.isPlaying()) {
                        plVideoView.start();
                    }
                });
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {

            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {

            }
        });

        rntShortVideoList.setLayoutManager(viewPagerLayoutManager);
        rntShortVideoList.setItemAnimator(new DefaultItemAnimator());
        rntShortVideoList.setAdapter(shortVideoAdapter);
    }
}
